package com.ruoyi.bpm.service.definition.impl;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.ruoyi.bpm.controller.admin.definition.vo.expression.BpmProcessExpressionPageReqVO;
import com.ruoyi.bpm.controller.admin.definition.vo.expression.BpmProcessExpressionSaveReqVO;
import com.ruoyi.bpm.domain.definition.BpmProcessExpression;
import com.ruoyi.bpm.mapper.definition.BpmProcessExpressionMapper;
import com.ruoyi.bpm.service.definition.BpmProcessExpressionService;
import com.ruoyi.bpm.utils.BeanUtils;
import com.ruoyi.common.orm.core.page.PageQuery;
import com.ruoyi.common.orm.core.page.TableDataInfo;
import com.ruoyi.common.orm.core.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import static com.ruoyi.bpm.domain.definition.table.BpmProcessExpressionTableDef.BPM_PROCESS_EXPRESSION;
import static com.ruoyi.bpm.enums.ErrorCodeConstants.PROCESS_EXPRESSION_NOT_EXISTS;
import static com.ruoyi.common.core.utils.ServiceExceptionUtil.exception;

/**
 * BPM 流程表达式 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class BpmProcessExpressionServiceImpl extends BaseServiceImpl<BpmProcessExpressionMapper, BpmProcessExpression> implements BpmProcessExpressionService {
    @Override
    public QueryWrapper query() {
        return super.query().from(BPM_PROCESS_EXPRESSION);
    }

    @Override
    public Long createProcessExpression(BpmProcessExpressionSaveReqVO createReqVO) {
        // 插入
        BpmProcessExpression processExpression = BeanUtils.toBean(createReqVO, BpmProcessExpression.class);
        mapper.insert(processExpression);
        // 返回
        return processExpression.getId();
    }

    @Override
    public void updateProcessExpression(BpmProcessExpressionSaveReqVO updateReqVO) {
        // 校验存在
        validateProcessExpressionExists(updateReqVO.getId());
        // 更新
        BpmProcessExpression updateObj = BeanUtils.toBean(updateReqVO, BpmProcessExpression.class);
        super.updateById(updateObj);
    }

    @Override
    public void deleteProcessExpression(Long id) {
        // 校验存在
        validateProcessExpressionExists(id);
        // 删除
        mapper.deleteById(id);
    }

    private void validateProcessExpressionExists(Long id) {
        if (this.getProcessExpression(id) == null) {
            throw exception(PROCESS_EXPRESSION_NOT_EXISTS);
        }
    }

    @Override
    public BpmProcessExpression getProcessExpression(Long id) {
        return super.getById(id);
    }

    @Override
    public TableDataInfo<BpmProcessExpression> getProcessExpressionPage(BpmProcessExpressionPageReqVO pageReqVO) {
        QueryWrapper queryWrapper = query()
            .where(BPM_PROCESS_EXPRESSION.NAME.like(pageReqVO.getName(), StrUtil.isNotBlank(pageReqVO.getName())))
            .and(BPM_PROCESS_EXPRESSION.STATUS.eq(pageReqVO.getStatus(), pageReqVO.getStatus() != null))
            .and(BPM_PROCESS_EXPRESSION.CREATE_TIME.between(pageReqVO.getCreateTime(), ArrayUtil.isNotEmpty(pageReqVO.getCreateTime())))
            .orderBy(BPM_PROCESS_EXPRESSION.ID.desc());
        Page<BpmProcessExpression> page = this.pageAs(PageQuery.build(), queryWrapper, BpmProcessExpression.class);
        return TableDataInfo.build(page);
    }

}
